<?php
namespace Admin\Controller;
use Think\Controller;

class IteminfoController extends Controller {

	function __construct() {
		//继承父类
		parent::__construct();

		//判断登录状态
		if(!D('Admin')->islogin()){//未登录
			$this->error('您尚未登录，请先登录！',U('login/login'),3);
		}
		//判断操作是否授权
		if(!D('Admin')->isoperate(CONTROLLER_NAME,ACTION_NAME)){//未授权
			$this->error('您尚无此操作权限！',U('index/welcome'),3);
		}
    }

	//空方法，防止报错
	public function _empty(){
        $this->index();
    }
	
	//评分项目表展示
    public function index(){
        $where="1 ";// and score=0
        $info=I("info");
        if ($info){
            @extract($info);
            if($keyword){
                $info['keyword']=urldecode($info['keyword']);
                $where.="and (item_name like '%".urldecode($info['keyword'])."%' or item_company like '%".urldecode($info['keyword'])."%' or item_linkmen like '%".urldecode($info['keyword'])."%' or item_linkphone like '%".urldecode($info['keyword'])."%') ";$this->assign('keyword',urldecode($info['keyword']));
            }
            if($starttime){
                $where.=" and ec_iteminfo.item_addtime>=".strtotime($starttime." 00:00:00")." ";
                $this->assign('starttime',$starttime);
            }
            if($endtime){
                $where.=" and ec_iteminfo.item_addtime<=".strtotime($endtime." 23:59:59")." ";
                $this->assign('endtime',$endtime);
            }
            if($indus_id){
                $where.=" and ec_iteminfo.indus_id=".$indus_id." ";
                $this->assign('indus_id',$indus_id);
            }
            if($item_province){
                $info['item_province']=urldecode($info['item_province']);
                $where.=" and ec_iteminfo.item_province='".urldecode($info['item_province'])."' ";
                $this->assign('item_province',urldecode($info['item_province']));
            }
            if($item_city){
                $info['item_city']=urldecode($info['item_city']);
                $where.=" and ec_iteminfo.item_city='".urldecode($info['item_city'])."' ";
                $this->assign('item_city',urldecode($info['item_city']));
            }
        }

        $Iteminfo=M('Iteminfo');
        // 查询满足要求的总记录数
        $count      = $Iteminfo->where($where)->count();
        $Page       = new \Think\Page($count,15);
        //分页跳转的时候保证查询条件
        if($info){
            foreach($info as $key=>$val) {
                $Page->parameter['info['.$key.']']   =   urlencode($val);
            }
        }
        $pageshow   = $Page->adminshow();
        // 进行分页数据查询
        $listinfo = $Iteminfo->where($where)->order('item_id desc')->join("left join ec_industry on ec_industry.indus_id=ec_iteminfo.indus_id")->limit($Page->firstRow.','.$Page->listRows)->field("ec_iteminfo.*,ec_industry.indus_title")->order("ec_iteminfo.item_id desc")->select();
        $this->assign('listinfo',$listinfo);
        $this->assign('pageshow',$pageshow);

        //行业分类信息
        $indusinfo=M("Industry")->where("indus_pid=0")->select();
        foreach($indusinfo as $k=>$v){
            $child=M("Industry")->where("indus_pid=".$v['indus_id'])->select();
            $indusinfo[$k]['child']=$child;
            $indusinfo[$k]['childnum']=sizeof($child);
        }

        $this->assign('indusinfo',$indusinfo);
        //省信息
        $provinces=M("Provinces")->where("1")->select();
        $this->assign('provinces',$provinces);

        $this->display();
    }
    //编辑项目信息
    public function edititem()
    {
        $item_id = I("get.item_id");
        if (IS_POST) {
            $info = I("info");
            $flag = M("iteminfo")->where("item_id=" . $item_id)->save($info);
            if ($flag) {
                $this->success('项目信息修改成功！', U('iteminfo/index'), 3);
            } else {
                $this->error('项目信息修改失败！', U('iteminfo/index'), 3);
            }
        } else {
            //基本信息预览展示
            $iteminfo = M("iteminfo")->where("item_id=" . $item_id)->find();
            $this->assign('iteminfo', $iteminfo);           /*这是由ID传给前端预显示的数组内容*/
            //行业分类信息
            $indusinfo = M("Industry")->where("indus_pid=0")->select();
            foreach ($indusinfo as $k => $v) {
                $child = M("Industry")->where("indus_pid=" . $v['indus_id'])->select();
                $indusinfo[$k]['child'] = $child;
                $indusinfo[$k]['childnum'] = sizeof($child);
            }
            $this->assign('indusinfo', $indusinfo);

            //省信息
            $provinces = M("Provinces")->where("1")->select();
            $this->assign('provinces', $provinces);
            //市信息
            $cities = M("cities")->where("1")->select();
            $this->assign('cities', $cities);
            $this->display();
        }
    }
    //增加项目信息
    public function additem(){
        //行业分类信息
        $indusinfo = M("Industry")->where("indus_pid=0")->select();
        foreach ($indusinfo as $k => $v) {
            $child = M("Industry")->where("indus_pid=" . $v['indus_id'])->select();
            $indusinfo[$k]['child'] = $child;
            $indusinfo[$k]['childnum'] = sizeof($child);
        }
        $this->assign('indusinfo', $indusinfo);
        //省信息
        $provinces = M("Provinces")->where("1")->select();
        $this->assign('provinces', $provinces);
        //市信息
        $cities = M("cities")->where("1")->select();
        $this->assign('cities', $cities);
        if(IS_POST){
            $info = I("info");
            $info['item_addtime']=time();
            $flag = M("iteminfo")->add($info);
            if ($flag) {
                $this->success('项目添加成功！', U('iteminfo/index'), 3);
            } else {
                $this->error('项目添加失败！', U('iteminfo/index'), 3);
            }
        }else{
            $this->display();
        }
    }
    //删除项目信息
//    public function delitem(){
//        $item_id = I("get.item_id");
//        $flag = M('iteminfo')->where("item_id=".$item_id)->delete();
//        if ($flag) {
//            $this->success('项目删除成功！', U('iteminfo/index'), 3);
//        } else {
//            $this->error('项目删除失败！', U('iteminfo/index'), 3);
//        }
//    }
    public function delitem(){
        $item_id=I("get.item_id");
        //删除项目表
        M("Iteminfo")->where("item_id=".$item_id)->delete();
        //删除项目评测各个项值以及分值
        M("Rating")->where("item_id=".$item_id)->delete();
        $this->success('删除成功',U('iteminfo/index'),3);
    }


}
